var t = getApp();

Page({
    data: {
        type: "news",
        typeArr: [ "", "news", "article", "audio", "video", "advideo" ],
        failPopup: !1,
        title: "",
        faceImg: "",
        textarea: "",
        stream_univalent: "",
        duration: "",
        link: "",
        setting: {},
        newsImageList: [],
        videoUrl: "",
        audioUrl: "",
        errorData: {},
        sendBtnFlag: !1
    },
    onLoad: function(t) {
        t.type && this.setData({
            type: t.type
        });
    },
    setFailPopup: function() {
        this.setData({
            failPopup: !this.data.failPopup
        });
    },
    updateFaceImage: function(t) {
        this.data.faceImg = t.detail[0] || "";
    },
    updateNewImageList: function(t) {
        this.data.newsImageList = t.detail;
    },
    successUpVideo: function(e) {
        var i = e.detail.res;
        if (1 != i.result) return t.tips(i.msg);
        this.setData({
            videoUrl: i.data.img_url
        });
    },
    successUpAudio: function(e) {
        var i = e.detail.res;
        if (1 != i.result) return t.tips(i.msg);
        this.setData({
            audioUrl: i.data.img_url
        });
    },
    onReady: function() {
        this.getSetting();
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    send: function() {
        var e = this;
        if (1 != this.data.sendBtnFlag) {
            var i = this.data, a = i.typeArr, n = i.type, s = i.faceImg, r = i.title, u = i.textarea, d = i.newsImageList, o = i.audioUrl, f = i.videoUrl, g = i.stream_univalent, l = i.duration, p = i.link, c = i.setting, v = {
                advertising_type: 1,
                genre: a.indexOf(n)
            };
            if (!s || "" == s) return t.tips("请上传封面");
            if (!r || "" == r) return t.tips("请填写标题");
            if (v.title = r, v.cover_picture = s, "news" == n || "article" == n) {
                if (!u || "" == u) return t.tips("请填写正文");
                v.content = u;
            }
            if ("news" == n) {
                if (!d || 0 == d.length) return t.tips("请上传图片");
                v.images = d;
            }
            if ("audio" == n) {
                if (!o || "" == o) return t.tips("请上传音频");
                v.resources_url = o;
            }
            if ("video" == n || "advideo" == n) {
                if (!f || "" == f) return t.tips("请上传视频");
                v.resources_url = f;
            }
            if ("advideo" == n) {
                if (!g || "" == g) return t.tips("请填写流量值单价");
                if (g < c.min_stream_hour) return t.tips("流量值单价不能小于最低值");
                if (!l || "" == l) return t.tips("请填写投放时长");
                v.advertising_type = 2, v.stream_univalent = g, v.duration = l, v.link = p;
            }
            this.data.sendBtnFlag = !0;
            var m = t.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.release-advertising");
            t._postNetWork({
                url: m,
                data: v,
                success: function(i) {
                    var a = i.data;
                    if (1 != a.result) return e.data.sendBtnFlag = !1, void ("2" == a.data.code ? e.setData({
                        failPopup: !0,
                        errorData: a.data
                    }) : t.tips(a.msg));
                    var n = getCurrentPages();
                    n[n.length - 2].getListData(), setTimeout(function() {
                        wx.navigateBack();
                    }, 500);
                },
                fail: function() {
                    this.data.sendBtnFlag = !1;
                }
            });
        }
    },
    getSetting: function() {
        var e = this, i = t.getNetAddresss("plugin.new-media-advertising.frontend.media-advertisingt.get-set");
        t._getNetWork({
            url: i,
            success: function(i) {
                var a = i.data;
                if (1 != a.result) return t.tips(a.msg);
                e.setData({
                    setting: a.data
                });
            }
        });
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});